﻿<script>
	var pub_ngapp = angular.module("ngapp", []);
	
	var pub_scope;
	pub_ngapp.controller('cashbook', function ($scope, $filter)
	{
		$scope.cash_date = new Date();

		func_page_init($scope, $filter);

		//var d = $filter("date")("2015-02-21", "yyyy-MM-dd") // yyyy/MM/dd指定转换的格式
	});
</script>


<script>
	/////////////////////////////////
	////页面初始化API
	function func_page_init(scope, filter)
	{
		var url = 'api__api.aspx';

		$.ajax({
			dataType: "json",
			url: url,
			data:
			    {
			    	__type: "func_page__cashbook__index",

			    	__d: (new Date()).valueOf()
			    },
			//////////////////////////////////////////////////
			success: function (data, textStatus, jqXHR)
			{
				setTimeout(function ()
				{
					pub_scope = scope;
					pub_scope.$apply(function ()
					{
						func_page_init__success(data, filter);
					});
				}, 100);
			},
			//////////////////////////////////////////////////
			error: function (data, textStatus, jqXHR)
			{
			}
		});
	}


	var pub_page_index = 0;
	var pub_page_size = 100;
	var pub_count = 0;
	var pub_click_month_cart = false;
	var pub_click_year_cart = false;
	function func_page_init__success(data, filter)
	{
		console.log(pub_scope);
		console.log(filter);


		pub_scope.cash_date = new Date();
//		pub_scope.cash_pay = data.page.cash_pay;
//		pub_scope.cash_income = data.page.cash_income;
		

		var yyyyMM = filter("date")(pub_scope.cash_date, "yyyy-MM");
		
		
		func_cashbook_list(yyyyMM, pub_page_index, pub_page_size);
		
		func_cashbook_sum(yyyyMM);
		
		
		pub_scope.ab_type_class = function(ab_type)
		{
			if(ab_type == "+")
			{
				return "text-success";
			}
			
			return "font-color-hui";
		}
		
		
		$(".more").click(function ()
		{
			var page_num = Math.floor(pub_count / pub_page_size);
			if (pub_count % pub_page_size != 0)
			{
				page_num++;
			}

			if (pub_page_index >= page_num)
			{
				func_alert("已经是最后一页了");
				return false;
			}


			var yyyyMM = filter("date")(pub_scope.cash_date, "yyyy-MM");
			func_cashbook_list(yyyyMM, pub_page_index, pub_page_size);
		});
		
		
		
		$(".month-cart").click(function ()
		{
			if(!pub_click_month_cart)
			{
				var yyyyMM = filter("date")(pub_scope.cash_date, "yyyy-MM");
				func_cashbook_cart__month(yyyyMM);
			}
		});
		
		
		$(".year-cart").click(function ()
		{
			if(!pub_click_year_cart)
			{
				var yyyyMM = filter("date")(pub_scope.cash_date, "yyyy");
				func_cashbook_cart__year(yyyyMM);
			}
		});
	}
</script>


<script>
	function func_cashbook_list__success(data, textStatus, jqXHR)
	{
		console.log(data);
		console.log(textStatus);
		console.log(jqXHR);

		if (data.errcode == 0)
		{
			pub_scope.$apply(function ()
			{								
				if(pub_scope.cashbook_list)
				{
					for (var i = 0; i < data.list.length; i++) 
					{
						pub_scope.cashbook_list.push(data.list[i]);
					}
				}
				else
				{
					pub_scope.cashbook_list=data.list;
				}
				

				pub_count = data.count;
				pub_page_index++;
			});
		}
		else
		{
			func_alert(data.errmsg);
		}
	}
	
		
	function func_cashbook_sum__success(data, textStatus, jqXHR)
	{
		console.log(data);
		console.log(textStatus);
		console.log(jqXHR);

		if (data.errcode == 0)
		{			
			pub_scope.$apply(function ()
			{
				pub_scope.cash_pay = data.cash_pay;
				pub_scope.cash_income = data.cash_income;
			});
		}
		else
		{
			func_alert(data.errmsg);
		}
	}
</script>



<script>
	function func_cashbook_cart__month__success(data, textStatus, jqXHR)
	{
		console.log(data);
		console.log(textStatus);
		console.log(jqXHR);

		if (data.errcode == 0)
		{
			//饼图
			//当月类型支出分布
			pub_click_month_cart = true;
			
			var chart4 = document.getElementById("pie-chart").getContext("2d");
			window.myPie = new Chart(chart4).Pie(data.list, {
				responsive: true
			});
		}
		else
		{
			func_alert(data.errmsg);
		}
	}
</script>


<script>
	function func_cashbook_cart__year__success(data, textStatus, jqXHR)
	{
		console.log(data);
		console.log(textStatus);
		console.log(jqXHR);

		if (data.errcode == 0)
		{
			pub_click_year_cart = true;			
			
			//线形图
			var lineChartData = {
				labels: data.list_month_str,
				datasets: [{
					label: "支出",
					fillColor: "rgba(220,220,220,0.2)",
					strokeColor: "rgba(220,220,220,1)",
					pointColor: "rgba(220,220,220,1)",
					pointStrokeColor: "#fff",
					pointHighlightFill: "#fff",
					pointHighlightStroke: "rgba(220,220,220,1)",
					data: data.list_pay_data //0-100表示
				}, {
					label: "收入",
					fillColor: "rgba(48, 164, 255, 0.2)",
					strokeColor: "rgba(48, 164, 255, 1)",
					pointColor: "rgba(48, 164, 255, 1)",
					pointStrokeColor: "#fff",
					pointHighlightFill: "#fff",
					pointHighlightStroke: "rgba(48, 164, 255, 1)",
					data: data.list_income_data
				}]
			}
			var chart1 = document.getElementById("line-chart").getContext("2d");
			window.myLine = new Chart(chart1).Line(lineChartData, {
				responsive: true
			});


			//条形图
			//每月收入与支出
			var barChartData = {
				labels: data.list_month_str,
				datasets: [{
					fillColor: "rgba(220,220,220,0.5)",
					strokeColor: "rgba(220,220,220,0.8)",
					highlightFill: "rgba(220,220,220,0.75)",
					highlightStroke: "rgba(220,220,220,1)",
					data: data.list_pay_data //0-100表示
				}, {
					fillColor: "rgba(48, 164, 255, 0.2)",
					strokeColor: "rgba(48, 164, 255, 0.8)",
					highlightFill: "rgba(48, 164, 255, 0.75)",
					highlightStroke: "rgba(48, 164, 255, 1)",
					data: data.list_income_data
				}]
			}
			
			var chart2 = document.getElementById("bar-chart").getContext("2d");
			window.myBar = new Chart(chart2).Bar(barChartData, {
				responsive: true
			});
		}
		else
		{
			func_alert(data.errmsg);
		}
	}
</script>



<script>
    $(function ()
    {
        $(".date").mobiscroll().date({
            theme: "android-ics light",
            lang: "zh",
            cancelText: null,
            dateFormat: 'yy-mm',
            animate: "flip",
            onBeforeShow: function (inst) { inst.settings.wheels[0].length>2?inst.settings.wheels[0].pop():null; }, //弹掉“日”滚轮  
            headerText: function (valueText)
            {
                //自定义弹出框头部格式  
                array = valueText.split('-');
                var date_str = array[0] + "年" + array[1] + "月";
           
                return date_str;
            },
            onSelect:function(valueText,inst)
            {
				 pub_scope.$apply(function ()
				{
					pub_scope.cash_date = new Date(valueText);
					
					//先清空数据
					pub_scope.cashbook_list = null;
					pub_page_index = 0;
					pub_page_size = 100;
					
					var yyyyMM = valueText;
					func_cashbook_list(yyyyMM, pub_page_index, pub_page_size);
					func_cashbook_sum(yyyyMM);	
				});
            }
        });
    })
</script>